package com.edu.d3_collections;

import com.edu.d1_set.Apple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class CollectionDemo2 {
    /**
     * 比较自定义类型的集合对象
     */
    public static void main(String[] args) {
/*        ArrayList<Apple> list = new ArrayList<>();
        list.add(new Apple("富士山","红苹果",99.8,100));
        list.add(new Apple("山","青苹果",66.3,600));
        list.add(new Apple("士山","黄苹果",22.5,300));
        list.add(new Apple("富","黑苹果",77.4,300));
        //第一种方法，重写compareTo方法进行排序
        Collections.sort(list);
        System.out.println(list);*/


//上面学的是TreeSet自定义比较规则的两种形式
        //本节学习的是，Collections工具类自定义比较规则的两种类型，形式类似
//第二种方法，用集合自带的比较规则，比较器
        ArrayList<Apple> list = new ArrayList<>();
        list.add(new Apple("富士山","红苹果",99.8,100));
        list.add(new Apple("山","青苹果",66.3,600));
        list.add(new Apple("士山","黄苹果",22.5,300));
        list.add(new Apple("富","黑苹果",77.4,300));
        System.out.println(list);
        Collections.sort(list,(apple, t1)->apple.getWeight()-t1.getWeight()>=0?1:-1);
/*        Collections.sort(list, new Comparator<Apple>() {
            @Override
            public int compare(Apple apple, Apple t1) {
                return apple.getWeight()-t1.getWeight()>=0?1:-1;
            }
        });*/
        System.out.println(list);
    }
}
